package br.ufla.pgcc.oomeasures.astvisitors;

import java.util.List;

import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;

public class MethodVisitor extends ASTVisitor {

	private static int numberOfProjectMethods;
	private static int numberOfProjectPrivateMethods;
	private static int numberOfProjectPublicMethods;

	private int numberOfClassPrivateMethods;
	private int numberOfClassProtectedMethods;
	private int numberOfClassPublicMethods;
	private int numberOfClassDefaultMethods;

	private int numberOfClassStaticMethods = 0; 
	private int numberOfClassFinalMethods = 0; 

	@SuppressWarnings("unchecked")
	@Override
	public boolean visit(MethodDeclaration method) {

		List<Modifier> modifiers = (List<Modifier>)method.modifiers();
		boolean hasVisibilityModifier = false;

		for (Object modifier : modifiers) {

			if (modifier instanceof Modifier){

				ModifierKeyword modKeyword = ((Modifier) modifier).getKeyword();
							
				if (modKeyword.equals(ModifierKeyword.STATIC_KEYWORD)) {
					numberOfClassStaticMethods++;
					
				} else if (modKeyword.equals(ModifierKeyword.FINAL_KEYWORD)) { 
					numberOfClassFinalMethods++;
					
				} else if (modKeyword.equals(ModifierKeyword.PRIVATE_KEYWORD)){
					numberOfClassPrivateMethods++;
					numberOfProjectPrivateMethods++;
					hasVisibilityModifier = true;
					
				} else if (modKeyword.equals(ModifierKeyword.PUBLIC_KEYWORD)) {
					numberOfClassPublicMethods++;
					numberOfProjectPublicMethods++;
					hasVisibilityModifier = true;
					
				} else if (modKeyword.equals(ModifierKeyword.PROTECTED_KEYWORD)){
					numberOfClassProtectedMethods++;
					hasVisibilityModifier = true;
				} 
			}
		}
		
		if (!hasVisibilityModifier) {
			numberOfClassDefaultMethods++;			
		}
		numberOfProjectMethods++;
		return false;
	}

	public int getNumberOfStaticMethods(){
		return numberOfClassStaticMethods;
	}

	public int getNumberOfFinalMethods(){
		return numberOfClassFinalMethods;
	}

	public int getNumberOfPrivateMethods() {
		return numberOfClassPrivateMethods;
	}

	public int getNumberOfProtectedMethods() {
		return numberOfClassProtectedMethods;
	}

	public int getNumberOfPublicMethods() {
		return numberOfClassPublicMethods;
	}

	public int getNumberOfDefaultMethods() {
		return numberOfClassDefaultMethods;
	}

	public static int getNumberOfProjectMethods() {
		return numberOfProjectMethods;
	}

	public static int getNumberOfProjectPrivateMethods() {
		return numberOfProjectPrivateMethods;
	}

	public static int getNumberOfProjectPublicMethods() {
		return numberOfProjectPublicMethods;
	}
} 
